package cn.iocoder.yudao.module.cook.service.tiaoliao;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.cook.controller.admin.tiaoliao.vo.*;
import cn.iocoder.yudao.module.cook.dal.dataobject.tiaoliao.TiaoliaoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.cook.dal.mysql.tiaoliao.TiaoliaoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cook.enums.ErrorCodeConstants.*;

/**
 * 食谱调料 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TiaoliaoServiceImpl implements TiaoliaoService {

    @Resource
    private TiaoliaoMapper tiaoliaoMapper;

    @Override
    public Long createTiaoliao(TiaoliaoSaveReqVO createReqVO) {
        // 插入
        TiaoliaoDO tiaoliao = BeanUtils.toBean(createReqVO, TiaoliaoDO.class);
        tiaoliaoMapper.insert(tiaoliao);
        // 返回
        return tiaoliao.getId();
    }

    @Override
    public void updateTiaoliao(TiaoliaoSaveReqVO updateReqVO) {
        // 校验存在
        validateTiaoliaoExists(updateReqVO.getId());
        // 更新
        TiaoliaoDO updateObj = BeanUtils.toBean(updateReqVO, TiaoliaoDO.class);
        tiaoliaoMapper.updateById(updateObj);
    }

    @Override
    public void deleteTiaoliao(Long id) {
        // 校验存在
        validateTiaoliaoExists(id);
        // 删除
        tiaoliaoMapper.deleteById(id);
    }

    private void validateTiaoliaoExists(Long id) {
        if (tiaoliaoMapper.selectById(id) == null) {
            throw exception(TIAOLIAO_NOT_EXISTS);
        }
    }

    @Override
    public TiaoliaoDO getTiaoliao(Long id) {
        return tiaoliaoMapper.selectById(id);
    }

    @Override
    public PageResult<TiaoliaoDO> getTiaoliaoPage(TiaoliaoPageReqVO pageReqVO) {
        return tiaoliaoMapper.selectPage(pageReqVO);
    }

}